### PACKAGE PREAMBLE ###

# Create a vector of package names
packages <- c("dplyr", "ggplot2", "tidyr", "tidyverse", "data.table", "readxl", "foreign", "devtools", "fixest", "sandwich", "modelsummary", "kableExtra")

installed_packages <- packages %in% rownames(installed.packages())
if (any(installed_packages == FALSE)) {
  install.packages(packages[!installed_packages])
}

# Packages loading
invisible(lapply(packages, library, character.only = TRUE))
rm(packages, installed_packages)

options(modelsummary_format_numeric_latex = "plain")
f <- function(x) format(x, digits = 3, nsmall = 2, scientific = FALSE)

getwd()
setwd(dir = "/Users/jmc4qg/The Lab Dropbox/Jonathan Colmer/ShotSpotter_env/Journal_submissions/ReStat/Replication Folder/")
mydata_drop_Winter <- read.dta("Analysis Data/NIBRS_analysis_drop_winter.dta")
mydata_drop_Spring <- read.dta("Analysis Data/NIBRS_analysis_drop_spring.dta")
mydata_drop_Summer <- read.dta("Analysis Data/NIBRS_analysis_drop_summer.dta")  
mydata_drop_Fall <- read.dta("Analysis Data/NIBRS_analysis_drop_fall.dta")  

# Panel A: Drop Winter

T1_DWinter <- feols(homicide_pc~ tMean + prec , data=mydata_drop_Winter)
summary(T1_DWinter)
T2_DWinter <- feols(homicide_pc~ tMean + prec | sample_month + week + dow, data=mydata_drop_Winter)
summary(T2_DWinter, cluster="fips")
T3_DWinter <- feols(homicide_pc~ tMean + prec | state_sample_month + week + dow, data=mydata_drop_Winter)
summary(T3_DWinter, cluster="fips")
T4_DWinter <- feols(homicide_pc~ tMean + prec | ori_sample_month + week + dow, data=mydata_drop_Winter)
summary(T4_DWinter, cluster="fips")

models <- list(T1_DWinter, T2_DWinter, T3_DWinter, T4_DWinter)
modelsummary(models, vcov = ~fips, output = "Figures and Tables/Table_A3/Table_A3_Panel_A.tex", fmt = f, stars = c('*' = 0.1, '**' = 0.05, '***' = 0.01))

# Panel A: Drop Spring

T1_DSpring <- feols(homicide_pc~ tMean + prec , data=mydata_drop_Spring)
summary(T1_DSpring)
T2_DSpring <- feols(homicide_pc~ tMean + prec | sample_month + week + dow, data=mydata_drop_Spring)
summary(T2_DSpring, cluster="fips")
T3_DSpring <- feols(homicide_pc~ tMean + prec | state_sample_month + week + dow, data=mydata_drop_Spring)
summary(T3_DSpring, cluster="fips")
T4_DSpring <- feols(homicide_pc~ tMean + prec | ori_sample_month + week + dow, data=mydata_drop_Spring)
summary(T4_DSpring, cluster="fips")

models <- list(T1_DSpring, T2_DSpring, T3_DSpring, T4_DSpring)
modelsummary(models, vcov = ~fips, output = "Figures and Tables/Table_A3/Table_A3_Panel_B.tex", fmt = f, stars = c('*' = 0.1, '**' = 0.05, '***' = 0.01))

# Panel A: Drop Summer

T1_DSummer <- feols(homicide_pc~ tMean + prec , data=mydata_drop_Summer)
summary(T1_DSummer)
T2_DSummer <- feols(homicide_pc~ tMean + prec | sample_month + week + dow, data=mydata_drop_Summer)
summary(T2_DSummer, cluster="fips")
T3_DSummer <- feols(homicide_pc~ tMean + prec | state_sample_month + week + dow, data=mydata_drop_Summer)
summary(T3_DSummer, cluster="fips")
T4_DSummer <- feols(homicide_pc~ tMean + prec | ori_sample_month + week + dow, data=mydata_drop_Summer)
summary(T4_DSummer, cluster="fips")

models <- list(T1_DSummer, T2_DSummer, T3_DSummer, T4_DSummer)
modelsummary(models, vcov = ~fips, output = "Figures and Tables/Table_A3/Table_A3_Panel_C.tex", fmt = f, stars = c('*' = 0.1, '**' = 0.05, '***' = 0.01))

# Panel D: Drop Fall

T1_DFall <- feols(homicide_pc~ tMean + prec , data=mydata_drop_Fall)
summary(T1_DFall)
T2_DFall <- feols(homicide_pc~ tMean + prec | sample_month + week + dow, data=mydata_drop_Fall)
summary(T2_DFall, cluster="fips")
T3_DFall <- feols(homicide_pc~ tMean + prec | state_sample_month + week + dow, data=mydata_drop_Fall)
summary(T3_DFall, cluster="fips")
T4_DFall <- feols(homicide_pc~ tMean + prec | ori_sample_month + week + dow, data=mydata_drop_Fall)
summary(T4_DFall, cluster="fips")

models <- list(T1_DFall, T2_DFall, T3_DFall, T4_DFall)
modelsummary(models, vcov = ~fips, output = "Figures and Tables/Table_A3/Table_A3_Panel_D.tex", fmt = f, stars = c('*' = 0.1, '**' = 0.05, '***' = 0.01))

